package com.hxd.otc.module.order.activity;

import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.event.Event;
import com.hxd.otc.module.coin_deal.bean.CoinDealRecoderBean;
import com.hxd.otc.module.order.bean.AdOrderDetailBean;
import com.hxd.otc.module.order.bean.AllOrderBean;
import com.hxd.otc.module.order.contract.OrderFinishedContract;
import com.hxd.otc.module.order.fragment.AllOrderFragment;
import com.hxd.otc.module.order.presenter.OrderFinishedPresenter;
import com.hxd.otc.utils.CommonUtil;
import com.hxd.otc.utils.DateUtil;
import com.hxd.otc.utils.StringUtil;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.BlocksToast;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 包  名 :  com.hxd.otc.module.order
 * 类  名 : SaleOrderDetailActivity
 * 作  者 :  zjl
 * 时  间 :  2018/6/5 17:19
 * 描  述 :  ${} 订单 完成页面
 */
public class OrderFinishedActivity extends BaseActivity<OrderFinishedPresenter> implements OrderFinishedContract.View {

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @BindView(R.id.tv_saller)
    TextView tvSaller;

    @BindView(R.id.tv_fee)
    TextView tvFee;

    @BindView(R.id.tv_fee_decs)
    TextView tvFeeDecs;

    @BindView(R.id.tv_order_amount)
    TextView tvOrderAmount;

    @BindView(R.id.tv_number_of_winning_above)
    TextView tvNumberOfTransactions;

    @BindView(R.id.tv_number_order)
    TextView tvNumberOrder;

    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;

    @BindView(R.id.tv_role)
    TextView tvRole;

    @BindView(R.id.tv_order_fee)
    TextView tvOrderFee;

    @BindView(R.id.tv_order_price)
    TextView tvOrderPrice;

    @BindView(R.id.tv_actual_transaction_amount)
    TextView tvActualTransactionAmount;

    @BindView(R.id.tv_actual_transaction_volume)
    TextView tvActualTransactionVolume;

    @BindView(R.id.ll_top)
    LinearLayout llTop;

    public static final String ISREFRESH_ORDER_LIST_DATA = "isRefresh";//是否通知去刷新 订单列表的数据
    public static final String ISNORMALORDER = "isNormalOrder";//
    public static final String COINDEALRECODERBEAN = "coinDealRecoderBean";//

    boolean isRefresh;//是否通知去刷新 订单列表的数据
    boolean isNormalOrder;


    AllOrderBean.DataBean orderBean;

    AdOrderDetailBean adOrderDetailBean;

    CoinDealRecoderBean.DataBean coinDealRecoderBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_finished;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        if (getIntent().getExtras().containsKey(COINDEALRECODERBEAN)) {
            coinDealRecoderBean = (CoinDealRecoderBean.DataBean) getIntent().getExtras().getSerializable(COINDEALRECODERBEAN);
            refreshCoinData(coinDealRecoderBean);
        } else {
            orderBean = (AllOrderBean.DataBean) getIntent().getExtras().getSerializable(AllOrderFragment.ORDERINFO);
            isNormalOrder = getIntent().getExtras().getBoolean(ISNORMALORDER);
            if (getIntent().getExtras().containsKey(ISREFRESH_ORDER_LIST_DATA)) {
                isRefresh = getIntent().getExtras().getBoolean(ISREFRESH_ORDER_LIST_DATA);
            }
            refreshLayout.autoRefresh(0, 10, 1);
            refreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                    mPresenter.doLoadData(orderBean.subOrderId);
                }
            });
        }

    }

    @OnClick({R.id.back_tv})
    public void onCliekEvent(View view) {
        switch (view.getId()) {
            case R.id.back_tv: //关闭当页面要发送给订单列表去更新数据
                if (orderBean != null && isRefresh) {
                    postSticky(new Event.RefreshOrderListEvent(orderBean.status, true));
                }

                finish();
                break;
        }
    }

    @Override
    protected void initEvent() {
        tvNumberOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtil.copyData(tvNumberOrder.getText().toString());
                BlocksToast.get().showToast(UIUtils.getContext().getString(R.string.copy_number_hint));
            }
        });
    }

    @Override
    protected OrderFinishedPresenter createPresenter() {
        return new OrderFinishedPresenter(this);
    }

    @Override
    public void onSetData(AllOrderBean.DataBean bean) {
        refreashData(bean);
    }

    @Override
    public void onAdSetData(AdOrderDetailBean bean) {
        refreshAdData(bean);
    }

    /**
     * 填充  广告商 完成页 数据
     *
     * @param bean
     */
    private void refreshAdData(AdOrderDetailBean bean) {
        llTop.setVisibility(View.VISIBLE);
        adOrderDetailBean = bean;
        if (adOrderDetailBean.orderVo.type == 1) {
            tvRole.setText(getString(R.string.buyer));
        } else {
            tvRole.setText(getString(R.string.seller));
        }

        String[] split = adOrderDetailBean.orderVo.transactionPair.split("/");
        tvOrderAmount.setText(getString(R.string.two_placeholder1,
                StringUtil.format2Number(adOrderDetailBean.orderVo.money), adOrderDetailBean.orderVo.legalCurrency));
        tvNumberOrder.setText(adOrderDetailBean.orderVo.orderNo);
        tvSaller.setText(adOrderDetailBean.buyVo.nickName);
        tvOrderFee.setText(getString(R.string.two_placeholder1, StringUtil.format4Number(adOrderDetailBean.orderVo.fee), split[0]));
        tvOrderTime.setText(adOrderDetailBean.orderVo.createTime);
        tvNumberOfTransactions.setText(getString(R.string.two_placeholder1, StringUtil.format2Number(adOrderDetailBean.orderVo.quantity), split[0]));
    }

    /**
     * 填充 币币交易完成页  数据
     *
     * @param coinBean
     */
    private void refreshCoinData(CoinDealRecoderBean.DataBean coinBean) {
        llTop.setVisibility(View.VISIBLE);
        if (coinBean.type == 0) {
            tvRole.setText(getString(R.string.buyer));
        } else {
            tvRole.setText(getString(R.string.seller));
        }

        String[] split = coinBean.transPair.split("/");
        tvOrderAmount.setText(getString(R.string.two_placeholder1, StringUtil.format2Number(coinBean.totalPrice), split[1]));
        tvNumberOrder.setText(coinBean.bbNo);
        tvSaller.setText(coinBean.nickName);
        tvOrderFee.setText(getString(R.string.two_placeholder1, StringUtil.format4Number(coinBean.fee), split[1]));
        tvOrderTime.setText(coinBean.createTime);
        tvNumberOfTransactions.setText(getString(R.string.two_placeholder1, StringUtil.format2Number(coinBean.transAmount), split[0]));
    }

    /**
     * 填充普通完成页  数据
     *
     * @param bean
     */
    private void refreashData(AllOrderBean.DataBean bean) {
        llTop.setVisibility(View.VISIBLE);
        orderBean = bean;
        if (bean.exType.equals("1")) {
            tvRole.setText(getString(R.string.buyer));
        } else {
            tvRole.setText(getString(R.string.seller));
        }

        tvNumberOrder.setText(bean.subOrderId);
        tvSaller.setText(bean.getAskNickName());

        if (bean.isNosenseOrder()) {//无感商订单
            if (UserHelper.isNonInductivePaySilver()) {//如果时候无感支付广告商 de 无感订单

//                tvFeeDecs.setVisibility(View.VISIBLE);
//                tvFeeDecs.setText(getResources().getStringArray(R.array.fee_decs)[1]);
                tvFee.setText(getString(R.string.profit_rebate));
                tvOrderFee.setText(getString(R.string.two_placeholder1,
                        StringUtil.handleZero(bean.rebateFee), bean.symbol));

            } else {//商户 de 无感订单

//                tvFeeDecs.setVisibility(View.GONE);
                tvFee.setText(getString(R.string.order_fee));

                tvOrderFee.setText(getString(R.string.two_placeholder1,
                        StringUtil.handleZero(bean.userFeeVolume), bean.symbol));
            }
        } else {//otc 订单
//            tvFeeDecs.setVisibility(View.GONE);
            tvFee.setText(getString(R.string.order_fee));

            if (orderBean.userId.equals(orderBean.orderUserId)) {//广告商
                tvOrderFee.setText(getString(R.string.two_placeholder1,
                        StringUtil.handleZero(bean.feeVolume), bean.symbol));
            } else {
                tvOrderFee.setText(getString(R.string.two_placeholder1,
                        "0", bean.symbol));
            }
        }
        tvOrderTime.setText(DateUtil.format(bean.createDate, DateUtil.YY_MM_DD_HH_MM_SS));
        tvOrderPrice.setText(getResources().getString(R.string.two_placeholder1,
                orderBean.price + "",
                orderBean.supportCurrencyCode + "/" + orderBean.symbol));

        tvActualTransactionAmount.setText(getString(R.string.two_placeholder1,
                StringUtil.format8Number(bean.totalPrice), bean.supportCurrencyCode));
        tvActualTransactionVolume.setText(getString(R.string.two_placeholder1,
                StringUtil.format8Number(bean.volume), bean.symbol));

        tvOrderAmount.setText(getString(R.string.two_placeholder1,
                StringUtil.format8Number(bean.orderTotalPrice), bean.supportCurrencyCode));
        tvNumberOfTransactions.setText(  getString(R.string.two_placeholder1,
                StringUtil.format8Number(bean.orderVolume), bean.symbol));

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isNormalOrder) {
                if (orderBean != null && isRefresh)
                    postSticky(new Event.RefreshOrderListEvent(orderBean.status, true));
            } else {
                if (adOrderDetailBean != null && adOrderDetailBean.orderVo != null && isRefresh)
                    postSticky(new Event.RefreshOrderListEvent(adOrderDetailBean.orderVo.status, true));
            }

        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onHideLoading() {
        super.onHideLoading();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onError(int code) {
        super.onError(code);
        refreshLayout.finishRefresh();
    }
}
